#include "EventWindow.h"

#include "EventAddDialog.h"
#include "EventEntry.h"
#include "EventLogonPage.h"
#include "EventMainPage.h"
#include "EventNetwork.h"
#include "GroupEntry.h"
#include "ObjectArray.h"

#include <libintl.h>
#define _(String) gettext(String)

struct _EventWindow
{
	AdwApplicationWindow parent_instance;

	GtkToggleButton	  *show_sidebar_button;
	GtkButton	  *event_add_button;
	GtkButton	  *go_to_logon_page_button;
	AdwWindowTitle	  *window_title;
	AdwNavigationView *user_interface_view;
	/* 登陆界面 */
	EventLogonPage *logon_page;
	/* Event 事件界面 */
	EventMainPage *main_page;
};

G_DEFINE_FINAL_TYPE(EventWindow, event_window, ADW_TYPE_APPLICATION_WINDOW)

static void
modify_sidebar_button_name_cb(EventWindow *self, GtkToggleButton *toggle)
{
	gboolean is_active;
	is_active = gtk_toggle_button_get_active(toggle);
	gtk_button_set_label(GTK_BUTTON(toggle),
			     is_active ? _("Fold") : _("Expand"));
	event_main_page_set_sidebar(self->main_page, is_active);
}

static void
go_to_logon_page_cb(EventWindow *self, gpointer data)
{
	event_network_clean_online_id(event_network);
	gtk_widget_set_visible(GTK_WIDGET(self->go_to_logon_page_button),
			       false);
	event_logon_page_set_page_type(self->logon_page,
				       EVENT_LOGON_PAGE_TYPE_LOGON);
}

static void
switch_to_main_page(EventWindow *self)
{
	const char *user_name;

	user_name = event_network_get_user_name(event_network);
	if (user_name) {
		EventString *__user_name_str;
		__user_name_str = event_string_new(_("Events - "));
		event_string_append(__user_name_str, user_name);
		adw_window_title_set_title(self->window_title,
					   ES_GET(__user_name_str));
		event_object_unref(__user_name_str);
	}
	adw_window_title_set_subtitle(self->window_title,
				      event_network_get_address(event_network));
	event_main_page_update_all_content(self->main_page);
	//
	gtk_widget_set_visible(GTK_WIDGET(self->event_add_button), true);
	gtk_widget_set_visible(GTK_WIDGET(self->show_sidebar_button), true);
	adw_navigation_view_push_by_tag(self->user_interface_view, "main-page");
}

static void
switch_to_logon_page(EventWindow *self)
{
	adw_window_title_set_title(self->window_title, _("Events"));
	adw_window_title_set_subtitle(self->window_title, "");
	gtk_widget_set_visible(GTK_WIDGET(self->event_add_button), false);
	gtk_widget_set_visible(GTK_WIDGET(self->show_sidebar_button), false);
	event_logon_page_clean_up_vcode(self->logon_page);
	event_network_clean_online_id(event_network);
	adw_navigation_view_pop_to_tag(self->user_interface_view, "login-page");
}

static void
event_logon_cb(EventWindow *self, EventLogonPage *logon_page)
{
	switch_to_main_page(self);
}

static void
add_event_window_cb(EventWindow *self, gpointer data)
{
	event_main_page_create_new_event(self->main_page);
}

static void
event_logout_action(GSimpleAction *action, GVariant *parameter,
		    gpointer user_data)
{
	EventWindow *self = EVENT_WINDOW(user_data);
	g_assert(EVENT_IS_WINDOW(user_data));
	switch_to_logon_page(self);
}

static void
event_fresh_action(GSimpleAction *action, GVariant *parameter,
		   gpointer user_data)
{
	EventWindow *self = EVENT_WINDOW(user_data);
	event_main_page_update_all_content(self->main_page);
}

static void
event_window_finalize(GObject *object)
{
	// EventWindow *self = EVENT_WINDOW(object);
	G_OBJECT_CLASS(event_window_parent_class)->finalize(object);
}

static void
event_window_class_init(EventWindowClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

	/* 踩坑：要想绑定 ui 文件中的自定义组件，比如 EventLogonPage，
           就必须先在类型系统中注册其信息。注册信息是靠
           event_logon_page_get_type()
           完成的，但在界面初始化时并没有调用它，必须手动注册类型信息。 */
	g_type_ensure(EVENT_TYPE_LOGON_PAGE);
	g_type_ensure(EVENT_TYPE_MAIN_PAGE);

	gtk_widget_class_set_template_from_resource(
		widget_class, "/org/korack/event/ui/EventWindow.ui");
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     show_sidebar_button);
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     event_add_button);
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     window_title);
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     go_to_logon_page_button);
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     user_interface_view);
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     logon_page);
	gtk_widget_class_bind_template_child(widget_class, EventWindow,
					     main_page);

	gtk_widget_class_bind_template_callback(widget_class,
						modify_sidebar_button_name_cb);
	gtk_widget_class_bind_template_callback(widget_class,
						add_event_window_cb);
	gtk_widget_class_bind_template_callback(widget_class,
						go_to_logon_page_cb);

	G_OBJECT_CLASS(klass)->finalize = event_window_finalize;
}

static const GActionEntry win_actions[] = {
	{ "logout", event_logout_action },
	{ "fresh",  event_fresh_action  },
};

static void
event_window_init(EventWindow *self)
{
	gtk_widget_init_template(GTK_WIDGET(self));

	g_action_map_add_action_entries(G_ACTION_MAP(self), win_actions,
					G_N_ELEMENTS(win_actions), self);

	/* 绑定 EventLogonPage 的信号 */
	g_signal_connect_swapped(self->logon_page, "logon",
				 G_CALLBACK(event_logon_cb), self);

	/* 登录界面收起无关按钮 */
	gtk_widget_set_visible(GTK_WIDGET(self->show_sidebar_button), false);
	gtk_widget_set_visible(GTK_WIDGET(self->event_add_button), false);
	event_logon_page_set_relate_return_button(
		self->logon_page, self->go_to_logon_page_button);
	event_logon_page_try_auto_logon(self->logon_page);
}
